package com.andy.qhb.action;

import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.dao.vo.Where;
import com.andy.qhb.entity.UsrFriends;
import com.andy.qhb.entity.UsrUser;
import com.andy.qhb.game.GameUtils;
import com.andy.qhb.game.GameUser;
import com.andy.qhb.game.Global;
import com.andy.qhb.utils.GmAction;
import com.andy.qhb.utils.SendUtil;
import com.andy.qhb.websocket.MySocketSession;

/**
 * 添加好友
 * @author cuiwm
 *
 */
@Component(GmAction.ACTION_PREFIX+GmAction.ACTION_111)
public class Action111 implements IWebAction{

	@Override
	public void doAction(MySocketSession myWebSocket, Map<String, Object> data) {
		
		GameUser user = myWebSocket.getUser();
		String phone = MapUtils.getString(data, "phone");
		
		UsrUser usrUser = DaoUtils.getOneSql(UsrUser.class, "select id,nick_name,icon from usr_user where phone=?", phone);
		if(usrUser == null){
			SendUtil.send111(myWebSocket,null);
			return ;
		}else{
			user.getFriends().addFriend(usrUser.getId(), usrUser.getNickName(),usrUser.getIcon());
			DaoUtils.update(user.getFriends());
			
			MySocketSession session = Global.getSession(user.getId());
			if(session != null && session.getUser() != null){
				session.getUser().getFriends().addFriend(user.getId(), user.getNickName(),user.getIcon());
				DaoUtils.update(session.getUser().getFriends());
			}else{
				// 走sql
				UsrFriends friends = DaoUtils.getOne(UsrFriends.class, Where.eq("uid", usrUser.getId()));
				if(friends != null){
					friends.setFids(friends.getFids()+user.getId()+",");
					DaoUtils.update(friends);
				}
			}
			SendUtil.send111(myWebSocket,usrUser);
		}
		
	}

	
	
	
}
